<?php

	class Metric {
		
		public function __construct() {
		
			$this->Id = 0;
			$this->UserId = "";
			$this->TeamName = "";
			$this->Datestamp = "";
			$this->FieldName = "";
			$this->Value = "";
		
		}

		function SetUserId($user_id) {
			$this->UserId = strtoupper(substr($user_id,0,50));
		}

		function SetTeamName($team_name) {
			$this->TeamName = strtoupper(substr($team_name,0,50));
		}

		function SetDatestamp($datestamp) {
			$this->Datestamp = $datestamp;
		}

		function SetFieldName($field_name) {
			$this->FieldName = strtoupper(substr($field_name,0,50));
		}

		function SetValue($value) {
			$this->Value = substr($value,0,255);
		}
		
		public function GetUserId() {
			return $this->UserId;
		}
		
		public function GetTeamName() {
			return $this->TeamName;
		}
		
		public function GetDatestamp() {
			return $this->Datestamp;
		}
		
		public function GetFieldName() {
			return $this->FieldName;
		}
		
		public function GetValue() {
			return $this->Value;	
		}

		function Load($user_id,$team_name,$datestamp,$field_name) {
		
			global $db_host, $db_username, $db_password, $db_database;
			
			$user_id = strtoupper(substr($user_id,0,50));
			$team_name = strtoupper(substr($team_name,0,50));
			$field_name = strtoupper(substr($field_name,0,50));
	
			mysql_connect($db_host,$db_username,$db_password);
			@mysql_select_db($db_database) or die("Unable to select database");
			$db_query = "SELECT * FROM metrics WHERE user_id = '$user_id' AND team_name = '$team_name' AND datestamp = '$datestamp' AND field_name = '$field_name';";
			$db_result = mysql_query($db_query);
			$db_count = mysql_num_rows($db_result);
			mysql_close();	
			
			if($db_count>0) {
			
				$metric_id		= mysql_result($db_result,0,"metric_id");
				$user_id		= mysql_result($db_result,0,"user_id");
				$team_name		= mysql_result($db_result,0,"team_name");
				$datestamp		= mysql_result($db_result,0,"datestamp");
				$field_name		= mysql_result($db_result,0,"field_name");
				$value			= mysql_result($db_result,0,"value");
				
				$this->Id = $metric_id;
				$this->UserId = strtoupper($user_id);	
				$this->TeamName = strtoupper($team_name);
				$this->Datestamp = $datestamp;
				$this->FieldName = strtoupper($field_name);
				$this->Value = $value;
				
				return true;			
	
			} else {
			
				$this->Id = 0;
				$this->UserId = "";	
				$this->TeamName = "";	
				$this->Datestamp = "";
				$this->FieldName = "";
				$this->Value = "";
								
				return false;
				
			}
		
		}
		
		function Save() {
		
			global $db_host, $db_username, $db_password, $db_database;
			
			$metric_id = $this->Id;
			$user_id = $this->UserId;
			$team_name = $this->TeamName;
			$datestamp = $this->Datestamp;
			$field_name = $this->FieldName;
			$value = $this->Value;
						
			$user_id = strtoupper(substr($user_id,0,50));
			$team_name = strtoupper(substr($team_name,0,50));
			$field_name = strtoupper(substr($field_name,0,50));	
					
			$db_link = mysql_connect($db_host,$db_username,$db_password);
			mysql_select_db($db_database) or die("Unable to select database");
			
			$db_query = "SELECT * FROM metrics WHERE user_id = '$user_id' AND team_name = '$team_name' AND datestamp = '$datestamp' AND field_name = '$field_name';";
			$db_result = mysql_query($db_query);
			@$db_count = mysql_num_rows($db_result);
			
			if($db_count==0) {
				
				$db_query = "INSERT INTO metrics (user_id,team_name,datestamp,field_name,value) VALUES ('$user_id','$team_name','$datestamp','$field_name','$value');";
				mysql_query($db_query);
				
			} else {
			
				$db_query = "UPDATE metrics SET user_id = '$user_id', team_name = '$team_name', field_name = '$field_name', value = '$value' WHERE user_id = '$user_id' AND team_name = '$team_name' AND datestamp = '$datestamp' AND field_name = '$field_name';";
				mysql_query($db_query);
			
			}
			
			mysql_close();
			
			return mysql_errno($db_link);
		
		}

		function Delete() {
		
			global $db_host, $db_username, $db_password, $db_database;
			
			$metric_id = $this->Id;			
					
			mysql_connect($db_host,$db_username,$db_password);
			mysql_select_db($db_database) or die("Unable to select database");
			$db_query = "DELETE FROM metrics WHERE metric_id = $metric_id;";
			mysql_query($db_query);
			mysql_close();
		
		}
		
	
	}

?>
